package com.start.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.admin.entity.StartSysMenu;
import com.start.admin.entity.StartSysRole;
import com.start.admin.mappers.SysRoleMapper;
import com.start.admin.mappers.SysRoleMenuMapper;
import com.start.admin.service.StartSysRoleService;
import com.start.common.exceptions.StartBusException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StartSysRoleServiceImpl extends ServiceImpl<SysRoleMapper, StartSysRole> implements StartSysRoleService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;



    @Override
    public StartSysRole getByCode(String code) {
        LambdaQueryWrapper<StartSysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StartSysRole::getRoleCode, code);
        return getOne(queryWrapper, false);
    }

    @Override
    public void createRole(StartSysRole role) {
        if (StringUtils.isEmpty(role.getRoleName())) throw new StartBusException("角色名称不能为空");
        if (StringUtils.isEmpty(role.getRoleCode())) throw new StartBusException("角色编码不能为空");
        save(role);
    }

    @Override
    public void updateRoleById(StartSysRole role) {
        if (StringUtils.isEmpty(role.getId())) throw new StartBusException("角色不存在");
        if (StringUtils.isEmpty(role.getRoleName())) throw new StartBusException("角色名称不能为空");
        if (StringUtils.isEmpty(role.getRoleCode())) throw new StartBusException("角色编码不能为空");
        updateById(role);
    }

    @Override
    public List<StartSysMenu> listRoleMenuByRoleId(String roleId) {
        return getBaseMapper().listRoleMenuByRoleId(roleId);
    }

    @Override
    @Transactional
    public void deleteRole(String roleId) {
        sysRoleMenuMapper.deleteByRoleId(roleId);
        removeById(roleId);
    }


}
